//
//  ZXStatusToolBar.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXStatusToolBar.h"

@interface ZXStatusToolBar()

@property(nonatomic,strong)NSMutableArray* btns;
@property(nonatomic,strong)NSMutableArray* lines;

@end

@implementation ZXStatusToolBar

-(instancetype)initWithFrame:(CGRect)frame
{
    self.btns = [NSMutableArray array];
    self.lines = [NSMutableArray array];
    
    
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"timeline_card_bottom_line_highlighted"]];
        [self addBtnWithIcon:@"timeline_icon_retweet" title:@" 转发"];
        [self addBtnWithIcon:@"timeline_icon_comment" title:@" 评论"];
        [self addBtnWithIcon:@"timeline_icon_unlike" title:@" 赞"];
        
//        添加两个分隔线
        [self addLine];
        [self addLine];
    }
    return self;
}
-(void)addLine
{
    UIImageView * line = [[UIImageView alloc]init];
    line.image = [UIImage imageNamed:@"navigationbar_background"];
    [self addSubview:line];
    [self.lines addObject:line];
}

-(void)addBtnWithIcon:(NSString *)icon title:(NSString *)title
{
    UIButton * btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:btn];
    [self.btns addObject:btn];

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger btncount = self.btns.count;
    
    CGFloat btnW = self.w / btncount;
    CGFloat btnH = self.h;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    for (NSInteger i = 0; i < btncount; i++)
    {
        UIButton * btn = self.btns[i];
        btnX = btnW * i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    
//    重新布局分割线
    NSInteger lineCount = self.lines.count;
    CGFloat lineX = 0;
    CGFloat lineY = 3;
    CGFloat lineW = 1;
    CGFloat lineH = self.h - 6;
    
    for (NSInteger i = 0; i < lineCount; i++)
    {
        UIImageView * line = self.lines[i];
        lineX = btnW * (i + 1);
        line.frame = CGRectMake(lineX, lineY, lineW, lineH);
    }
}

-(void)setStatus:(ZXStatuses *)status
{
    [self setBtnTitleWithCount:status.reposts_count defaultTitle:@"转发" btnIndex:0];
    [self setBtnTitleWithCount:status.comments_count defaultTitle:@"评论" btnIndex:1];
    [self setBtnTitleWithCount:status.attitudes_count defaultTitle:@"赞" btnIndex:2];
}
-(void)setBtnTitleWithCount:(int)count defaultTitle:(NSString *)defaultTitle btnIndex:(NSInteger)btnIndex
{
    UIButton *btn = self.btns[btnIndex];
    if (count >= 10000) {
        CGFloat tmpCount =  count / 10000.00;
        
        defaultTitle = [NSString stringWithFormat:@"%.2f万",tmpCount];
        defaultTitle = [defaultTitle stringByReplacingOccurrencesOfString:@".00" withString:@""];
        
    }else if(count > 0){
        defaultTitle = [NSString stringWithFormat:@"%d",count];
    }
    
    [btn setTitle:defaultTitle forState:UIControlStateNormal];
}
@end
